package com.android.cargoapp.cargoapp_gui_prototype;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.slidingmenu.lib.app.SlidingActivity;

public class MainActivity extends SlidingActivity{
	
	private float d = 0;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);
		
		__showWeight(0);
		
		// SlidingMenu
		setBehindContentView(R.layout.activity_menu);
		getSlidingMenu().setBehindWidth(300);
		
		String[] menu_items_array = getResources().getStringArray(R.array.menu_entries);
		ArrayAdapter<String> menu_list_adapter = new ArrayAdapter<String>(this, R.layout.menu_item,menu_items_array);
		
		ListView menu = (ListView) findViewById(R.id.menu_lv);
		menu.setAdapter(menu_list_adapter);
		
		getSlidingMenu().setFadeDegree(0.35f);
		
		__setMenuOnClickListener(this, menu);
		
		final Context context = this;
		((Button) findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String ein = ((EditText) findViewById(R.id.editText1)).getText().toString();
				try{
					float einf = Float.parseFloat(ein);
					if(einf > 0) {
						// Bild/Pfeil vom Tachometer drehen
						float tmp = d;
						if(d+einf > 180) d=180; else d += einf;
						RotateAnimation ranim = new RotateAnimation(tmp, d, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF, 0.83333333f);
						ranim.setFillAfter(true);
						ranim.setInterpolator(new LinearInterpolator());
						ranim.setDuration(800);
						
						__showWeight(d);
						
						// if(d >= Math.abs(360)) d -= 360;
						
						((ImageView)findViewById(R.id.image)).startAnimation(ranim);
					} else {
						throw new NumberFormatException("Nep");
					}
				} catch(NumberFormatException e){
					Toast.makeText(context, "Eine positive Zahl > 0 eingeben!", Toast.LENGTH_LONG).show();
				}
			}
		});
		
		((Button) findViewById(R.id.button2)).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				float tmp = d;
				d = 0;
				RotateAnimation ranim = new RotateAnimation(tmp, d, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF, 0.83333333f);
				ranim.setFillAfter(true);
				ranim.setInterpolator(new LinearInterpolator());
				ranim.setDuration(800);
				((ImageView)findViewById(R.id.image)).startAnimation(ranim);
				__showWeight(d);
			}
		});
		
		((Button) findViewById(R.id.button3)).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String ein = ((EditText) findViewById(R.id.editText1)).getText().toString();
				try{
					float einf = Float.parseFloat(ein);
					if(einf > 0) {
						// Bild/Pfeil vom Tachometer drehen
						float tmp = d;
						if(d-einf < 0) d=0; else d -= einf;
						RotateAnimation ranim = new RotateAnimation(tmp, d, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF, 0.83333333f);
						ranim.setFillAfter(true);
						ranim.setInterpolator(new LinearInterpolator());
						ranim.setDuration(800);
						
						__showWeight(d);
						
						// if(d >= Math.abs(360)) d -= 360;
						
						((ImageView)findViewById(R.id.image)).startAnimation(ranim);
					} else {
						throw new NumberFormatException("Nep");
					}
				} catch(NumberFormatException e){
					Toast.makeText(context, "Eine positive Zahl > 0 eingeben!", Toast.LENGTH_LONG).show();
				}
			}
		});
		
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		getSlidingMenu().showMenu();
		return super.onMenuItemSelected(featureId, item);
	}
	
	private void __setMenuOnClickListener(final Context c, ListView menu){

		menu.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View item, int position,
					long id) {
				Intent i = new Intent();
				i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				switch (position) {
				case 0:
					i.putExtra("name", "Mein Opel");
					i.setClassName("com.android.cargoapp.cargoapp_gui_prototype", MeinOpel.class.getName());
					startActivity(i);
					break;
					
				case 1:
					i = new Intent();
					i.setAction("com.android.cargoapp.cargoapp_gui_prototype.settings.START_SETTINGS");
					// i.setClassName("com.android.cargoapp.cargoapp_gui_prototype.settings", com.android.cargoapp.cargoapp_gui_prototype.settings.Settings.class.getName());
					startActivity(i);
					break;
					
				default:
					Toast.makeText(c, "Nanananana :P", Toast.LENGTH_LONG).show();
					break;
				}
				
			}
		});
	}
	
	private void __showWeight(float f){
		((TextView) findViewById(R.id.textView1)).setText(Float.toString(f));
	}
	
}
